package com.smart.reader.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.smart.reader.common.Page;
import com.smart.reader.exception.CustomException;
import com.smart.reader.mapper.ClassInfoMapper;
import com.smart.reader.mapper.StudentInfoMapper;
import com.smart.reader.model.bo.StudentInfoAddBo;
import com.smart.reader.model.bo.StudentInfoUpdateBo;
import com.smart.reader.model.po.ClassInfo;
import com.smart.reader.model.po.StudentInfo;
import com.smart.reader.model.vo.StudentInfoVo;
import com.smart.reader.service.StudentInfoService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@Service
public class StudentInfoServiceImpl extends ServiceImpl<StudentInfoMapper, StudentInfo> implements StudentInfoService {
    
    @Resource
    private ClassInfoMapper classInfoMapper;
    
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean add(StudentInfoAddBo studentInfo) throws CustomException {
        // 验证班级是否存在
        ClassInfo classInfo = classInfoMapper.selectById(studentInfo.getClassInfoId());
        if (ObjectUtil.isEmpty(classInfo)) {
            throw new CustomException("所选班级不存在");
        }
        
        StudentInfo info = BeanUtil.copyProperties(studentInfo, StudentInfo.class);
        boolean save = this.save(info);
        if (!save) {
            throw new CustomException("学生数据添加失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public Page<StudentInfoVo> queryInfo(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<StudentInfoVo> studentInfoVos = this.baseMapper.queryInfo();
        return new Page<>(pageNum, pageSize, studentInfoVos);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long id) throws CustomException {
        boolean remove = this.removeById(id);
        if (!remove) {
            throw new CustomException("学生数据删除失败");
        }
        return Boolean.TRUE;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean edit(@Valid StudentInfoUpdateBo studentInfo) throws CustomException {
        StudentInfo info = this.getById(studentInfo.getId());
        if (ObjectUtil.isEmpty(info)) {
            throw new CustomException("学生数据不存在");
        }
        
        // 如果修改了班级，验证新班级是否存在
        if (studentInfo.getClassInfoId() != null && !studentInfo.getClassInfoId().equals(info.getClassInfoId())) {
            ClassInfo classInfo = classInfoMapper.selectById(studentInfo.getClassInfoId());
            if (ObjectUtil.isEmpty(classInfo)) {
                throw new CustomException("所选班级不存在");
            }
        }
        
        BeanUtil.copyProperties(studentInfo, info);
        boolean update = this.updateById(info);
        if (!update) {
            throw new CustomException("学生数据修改失败");
        }
        return Boolean.TRUE;
    }

    @Override
    public StudentInfoVo detail(Long id) {
        return this.baseMapper.detail(id);
    }
    
    @Override
    public List<StudentInfoVo> queryByClassId(Long classInfoId) {
        return this.baseMapper.queryByClassId(classInfoId);
    }
}
